--
-- PostgreSQL database dump
--

-- Started on 2010-07-09 16:02:19

SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

--
-- TOC entry 1 (class 2615 OID 1054643)
-- Name: mtl; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA mtl;


--
-- TOC entry 3 (class 2615 OID 1058795)
-- Name: sys; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA sys;


--
-- TOC entry 427 (class 2612 OID 1060688)
-- Name: plpgsql; Type: PROCEDURAL LANGUAGE; Schema: -; Owner: -
--

CREATE PROCEDURAL LANGUAGE plpgsql;


SET search_path = mtl, pg_catalog;

--
-- TOC entry 50 (class 1255 OID 1100693)
-- Dependencies: 1
-- Name: add_artist_comment(integer, integer, text, cidr); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION add_artist_comment(integer, integer, text, cidr) RETURNS void
    LANGUAGE sql SECURITY DEFINER
    AS $_$
	INSERT INTO mtl.artists_comments (artist_id, added_by_user_id, message, ip) VALUES ($1, $2, $3, $4);
$_$;


--
-- TOC entry 89 (class 1255 OID 1160427)
-- Dependencies: 1
-- Name: add_release_comment(integer, integer, text, cidr); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION add_release_comment(integer, integer, text, cidr) RETURNS void
    LANGUAGE sql SECURITY DEFINER
    AS $_$
	INSERT INTO mtl.releases_comments (release_id, added_by_user_id, message, ip) VALUES ($1, $2, $3, $4);
$_$;


--
-- TOC entry 25 (class 1255 OID 1100002)
-- Dependencies: 427 1
-- Name: artist_has_schedule(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION artist_has_schedule(artist_id integer) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
BEGIN

	SELECT id FROM mtl.artists_rel_release WHERE (SELECT schedule_date FROM mtl.releases WHERE id = release_id) IS NOT NULL AND artist_id = $1;

	IF NOT FOUND THEN
		RETURN FALSE;
	ELSE
		RETURN TRUE;
	END IF;
	
	RETURN FALSE;

END;
$_$;


--
-- TOC entry 76 (class 1255 OID 1123504)
-- Dependencies: 427 1
-- Name: artist_rel_label(integer, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION artist_rel_label(_artist_id integer, _label_name text) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
BEGIN
	PERFORM * FROM mtl.artists_rel_label 
	INNER JOIN mtl.labels ON artists_rel_label.label_id = labels.id
	 WHERE artist_id = $1 AND name = $2;

	 IF found THEN
		RETURN TRUE;
	 ELSE
		RETURN FALSE;
	END IF;
END;
$_$;


--
-- TOC entry 74 (class 1255 OID 1123136)
-- Dependencies: 427 1
-- Name: artist_rel_release(integer, text, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION artist_rel_release(_artist_id integer, _release_name text, _year_finished integer) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
BEGIN
	PERFORM * FROM mtl.artists_rel_release 
	INNER JOIN mtl.releases ON artists_rel_release.release_id = releases.id
	 WHERE artist_id = $1 AND name = $2 AND year_finished = $3;

	 IF found THEN
		RETURN TRUE;
	 ELSE
		RETURN FALSE;
	END IF;
END;
$_$;


--
-- TOC entry 88 (class 1255 OID 1190907)
-- Dependencies: 1 427
-- Name: artist_rel_release_no_year(integer, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION artist_rel_release_no_year(_artist_id integer, _release_name text) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
BEGIN
	PERFORM * FROM mtl.artists_rel_release 
	INNER JOIN mtl.releases ON artists_rel_release.release_id = releases.id
	 WHERE artist_id = $1 AND name = $2;

	 IF found THEN
		RETURN TRUE;
	 ELSE
		RETURN FALSE;
	END IF;
END;
$_$;


--
-- TOC entry 75 (class 1255 OID 1123137)
-- Dependencies: 1 427
-- Name: artist_rel_tag(integer, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION artist_rel_tag(_artist_id integer, _tag_name text) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
BEGIN
	PERFORM * FROM mtl.artists_rel_tag 
	INNER JOIN mtl.tags ON artists_rel_tag.tag_id = tags.id
	 WHERE artist_id = $1 AND name = $2;

	 IF found THEN
		RETURN TRUE;
	 ELSE
		RETURN FALSE;
	END IF;
END;
$_$;


--
-- TOC entry 59 (class 1255 OID 1106647)
-- Dependencies: 427 1
-- Name: create_artist_stats(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION create_artist_stats() RETURNS "trigger"
    LANGUAGE plpgsql STRICT
    AS $$BEGIN
     INSERT INTO mtl.artists_stats (artist_id) VALUES (TG_ARGV[0]);
END
$$;


--
-- TOC entry 65 (class 1255 OID 1106661)
-- Dependencies: 1 427
-- Name: create_release_stats(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION create_release_stats() RETURNS "trigger"
    LANGUAGE plpgsql
    AS $$
BEGIN
	INSERT INTO mtl.releases_stats (release_id) VALUES (id);
END
$$;


--
-- TOC entry 40 (class 1255 OID 1100394)
-- Dependencies: 1
-- Name: create_wiki_for_artist(integer, integer, text, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION create_wiki_for_artist(artist_id integer, user_id integer, wiki text, source text) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	INSERT INTO mtl.artists_wiki(artist_id, added_by_user_id, data, source) VALUES ($1, $2, $3, $4);
$_$;


--
-- TOC entry 90 (class 1255 OID 1092364)
-- Dependencies: 1
-- Name: delete_artist_cascade(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION delete_artist_cascade(artist_id integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	-- remove artist
	DELETE FROM mtl.artists WHERE id = $1;

	-- remove artist releases
	DELETE FROM mtl.releases WHERE id IN (SELECT release_id FROM mtl.artists_rel_release WHERE artist_id = $1);

	-- remove artist tags
	DELETE FROM mtl.tags WHERE id IN (SELECT tag_id FROM mtl.artists_rel_tag WHERE artist_id = $1) AND used < 2;

	-- remove artist members
	DELETE FROM mtl.members WHERE id IN (SELECT member_id FROM mtl.artists_rel_member WHERE artist_id = $1) AND used < 2;

	-- remove artist labels
	DELETE FROM mtl.labels WHERE id IN (SELECT label_id FROM mtl.artists_rel_label WHERE artist_id = $1) AND used < 2;

	DELETE FROM mtl.members_rel_role WHERE member_id IN (SELECT member_id FROM mtl.artists_rel_member WHERE artist_id = $1);

	-- remove artist relations
	DELETE FROM mtl.artists_rel_release WHERE artist_id = $1;
	DELETE FROM mtl.artists_rel_tag WHERE artist_id = $1;
	DELETE FROM mtl.artists_rel_member WHERE artist_id = $1;
	DELETE FROM mtl.artists_rel_label WHERE artist_id = $1;

	DELETE FROM mtl.labels_rel_tag WHERE label_id IN (SELECT label_id FROM mtl.artists_rel_label WHERE artist_id = $1);

	

$_$;


--
-- TOC entry 61 (class 1255 OID 1106653)
-- Dependencies: 1 427
-- Name: delete_artist_comments(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION delete_artist_comments() RETURNS "trigger"
    LANGUAGE plpgsql
    AS $$
BEGIN
	DELETE FROM mtl.artists_comments WHERE artist_id = id;
END
$$;


--
-- TOC entry 63 (class 1255 OID 1106651)
-- Dependencies: 427 1
-- Name: delete_artist_relations(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION delete_artist_relations() RETURNS "trigger"
    LANGUAGE plpgsql
    AS $$
BEGIN
	DELETE FROM mtl.releases WHERE id IN (SELECT release_id FROM mtl.artists_rel_release WHERE artist_id = id);
	DELETE FROM mtl.artists_rel_release WHERE artist_id = id;
	DELETE FROM mtl.artists_rel_label WHERE artist_id = id;
	DELETE FROM mtl.artists_rel_member WHERE artist_id = id;
	DELETE FROM mtl.artists_rel_tag WHERE artist_id = id;
END
$$;


--
-- TOC entry 60 (class 1255 OID 1106649)
-- Dependencies: 1 427
-- Name: delete_artist_stats(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION delete_artist_stats() RETURNS "trigger"
    LANGUAGE plpgsql
    AS $$
BEGIN
	DELETE FROM mtl.artists_stats WHERE id = id;
END
$$;


--
-- TOC entry 62 (class 1255 OID 1106655)
-- Dependencies: 427 1
-- Name: delete_artist_wiki(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION delete_artist_wiki() RETURNS "trigger"
    LANGUAGE plpgsql
    AS $$
BEGIN
	DELETE FROM mtl.artists_wiki WHERE artist_id = id;
END
$$;


--
-- TOC entry 64 (class 1255 OID 1106659)
-- Dependencies: 427 1
-- Name: delete_release_relations(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION delete_release_relations() RETURNS "trigger"
    LANGUAGE plpgsql
    AS $$
BEGIN
	DELETE FROM mtl.releases_rel_label WHERE release_id = id;
	DELETE FROM mtl.releases_rel_tag WHERE release_id = id;
END
$$;


--
-- TOC entry 66 (class 1255 OID 1106663)
-- Dependencies: 427 1
-- Name: delete_release_stats(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION delete_release_stats() RETURNS "trigger"
    LANGUAGE plpgsql
    AS $$
BEGIN
	DELETE FROM mtl.releases_stats WHERE id = id;
END
$$;


--
-- TOC entry 22 (class 1255 OID 1099980)
-- Dependencies: 1 427
-- Name: generate_tba_release_name(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION generate_tba_release_name() RETURNS text
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $$
DECLARE
	result text;
BEGIN
	result = 'tba' || nextval('mtl.tba_release_id_seq')::text;

	RETURN result;	
END;
$$;


--
-- TOC entry 43 (class 1255 OID 1058583)
-- Dependencies: 1
-- Name: get_artist_id_by_name(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_artist_id_by_name(artistname text) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT id FROM mtl.artists WHERE ("name" = $1) LIMIT 1;
$_$;


--
-- TOC entry 29 (class 1255 OID 1085369)
-- Dependencies: 1
-- Name: get_artist_id_by_release_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_artist_id_by_release_id(release_id integer) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT artist_id FROM mtl.artists_rel_release WHERE (release_id = $1) LIMIT 1;
$_$;


--
-- TOC entry 30 (class 1255 OID 1085371)
-- Dependencies: 1
-- Name: get_artist_name_by_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_artist_name_by_id(label_id integer) RETURNS character
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT "name" FROM mtl.artists WHERE (id = $1) LIMIT 1;
$_$;


--
-- TOC entry 87 (class 1255 OID 1220519)
-- Dependencies: 427 1
-- Name: get_artist_schedule_info(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_artist_schedule_info(artist_id integer) RETURNS text
    LANGUAGE plpgsql
    AS $_$
declare
	sch_id bigint;
	result text;
	rls record;
begin

	SELECT rel.release_id INTO sch_id FROM mtl.artists_rel_release as rel LEFT JOIN mtl.releases as r ON (r.id = rel.release_id) WHERE rel.artist_id = $1 AND r.schedule_date IS NOT NULL;

	IF NOT FOUND THEN
		RETURN '';
	ELSE
		SELECT * INTO rls FROM mtl.schedules_view WHERE id = sch_id;
		result := rls.schedule_date || ' (' || rls.name || ')';
		RETURN result;
	END IF;

	RETURN '';
END;
$_$;


--
-- TOC entry 46 (class 1255 OID 1060204)
-- Dependencies: 1
-- Name: get_label_id_by_name(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_label_id_by_name(labelname text) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT id FROM mtl.labels WHERE ("name" = $1) LIMIT 1;
$_$;


--
-- TOC entry 37 (class 1255 OID 1076019)
-- Dependencies: 1
-- Name: get_label_name_by_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_label_name_by_id(label_id integer) RETURNS character
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT "name" FROM mtl.labels WHERE (id = $1) LIMIT 1;
$_$;


--
-- TOC entry 45 (class 1255 OID 1058793)
-- Dependencies: 1
-- Name: get_member_id_by_name(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_member_id_by_name(membername text) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT id FROM mtl.members WHERE ("name" = $1) LIMIT 1;
$_$;


--
-- TOC entry 32 (class 1255 OID 1085394)
-- Dependencies: 1
-- Name: get_record_type_id_by_name(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_record_type_id_by_name(record_type_name text) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT id FROM mtl.record_types WHERE ("name" = $1) LIMIT 1;
$_$;


--
-- TOC entry 31 (class 1255 OID 1085393)
-- Dependencies: 1
-- Name: get_record_type_name_by_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_record_type_name_by_id(record_type_id integer) RETURNS character
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT "name" FROM mtl.record_types WHERE (id = $1) LIMIT 1;
$_$;


--
-- TOC entry 20 (class 1255 OID 1085222)
-- Dependencies: 1
-- Name: get_release_count_by_artist_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_release_count_by_artist_id(artist_id integer) RETURNS bigint
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT COUNT(*) FROM mtl.artists_rel_release WHERE (artist_id = $1) AND NOT mtl.is_scheduled_release(release_id);
$_$;


--
-- TOC entry 42 (class 1255 OID 1075386)
-- Dependencies: 1
-- Name: get_release_id_by_name(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_release_id_by_name(releasename text) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT id FROM mtl.releases WHERE ("name" = $1) LIMIT 1;
$_$;


--
-- TOC entry 35 (class 1255 OID 1076022)
-- Dependencies: 1
-- Name: get_release_name_by_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_release_name_by_id(release_id integer) RETURNS character
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT "name" FROM mtl.releases WHERE (id = $1) LIMIT 1;
$_$;


--
-- TOC entry 36 (class 1255 OID 1076025)
-- Dependencies: 1
-- Name: get_release_year_by_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_release_year_by_id(release_id integer) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT year_finished FROM mtl.releases WHERE (id = $1) LIMIT 1;
$_$;


--
-- TOC entry 47 (class 1255 OID 1066216)
-- Dependencies: 1
-- Name: get_role_id_by_name(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_role_id_by_name(rolename text) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT id FROM mtl.roles WHERE ("name" = $1) LIMIT 1;
$_$;


--
-- TOC entry 44 (class 1255 OID 1058584)
-- Dependencies: 1
-- Name: get_tag_id_by_name(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_tag_id_by_name(tagname text) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT id FROM mtl.tags WHERE ("name" = $1) LIMIT 1;
$_$;


--
-- TOC entry 34 (class 1255 OID 1076021)
-- Dependencies: 1
-- Name: get_tag_name_by_id(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION get_tag_name_by_id(tag_id integer) RETURNS character
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT "name" FROM mtl.tags WHERE (id = $1) LIMIT 1;
$_$;


--
-- TOC entry 54 (class 1255 OID 1101180)
-- Dependencies: 427 1
-- Name: is_schedule_date_ym(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION is_schedule_date_ym(text) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE SECURITY DEFINER
    AS $_$
BEGIN

IF length(substring($1, '^[0-9]{4}-[0-9]{2}')) > 0 THEN
	RETURN TRUE;
ELSE
	RETURN FALSE;
END IF;

END;
$_$;


--
-- TOC entry 53 (class 1255 OID 1101179)
-- Dependencies: 1 427
-- Name: is_schedule_date_ymd(text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION is_schedule_date_ymd(text) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE SECURITY DEFINER
    AS $_$
BEGIN

IF length(substring($1, '[0-9]{4}-[0-9]{2}-[0-9]{2}')) = 10 THEN
	RETURN TRUE;
ELSE
	RETURN FALSE;
END IF;

END;
$_$;


--
-- TOC entry 85 (class 1255 OID 1092146)
-- Dependencies: 427 1
-- Name: is_scheduled_release(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION is_scheduled_release(release_id integer) RETURNS boolean
    LANGUAGE plpgsql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
DECLARE
	r character varying;
BEGIN

	SELECT schedule_date INTO r FROM mtl.releases WHERE id = $1 AND schedule_date IS NOT NULL LIMIT 1;

	IF NOT FOUND THEN
		RETURN FALSE;
	ELSE
		RETURN TRUE;
	END IF;
	
	RETURN FALSE;

END;
$_$;


--
-- TOC entry 67 (class 1255 OID 1122938)
-- Dependencies: 1
-- Name: link_artist_with_label(integer, integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_artist_with_label(artist_id integer, label_id integer, year integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE mtl.labels SET used = (used + 1) WHERE id = $2;
	INSERT INTO mtl.artists_rel_label (artist_id, label_id, "year") VALUES ($1, $2, $3);
$_$;


--
-- TOC entry 68 (class 1255 OID 1122940)
-- Dependencies: 1
-- Name: link_artist_with_member(integer, integer, integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_artist_with_member(artist_id integer, member_id integer, year_started integer, year_finished integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE mtl.members SET used = (used + 1) WHERE id = $2;
	INSERT INTO mtl.artists_rel_member (artist_id, member_id, year_started, year_finished) VALUES ($1, $2, $3, $4);
$_$;


--
-- TOC entry 69 (class 1255 OID 1122941)
-- Dependencies: 1
-- Name: link_artist_with_release(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_artist_with_release(artist_id integer, release_id integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	INSERT INTO mtl.artists_rel_release (artist_id, release_id) VALUES ($1, $2);
$_$;


--
-- TOC entry 70 (class 1255 OID 1122944)
-- Dependencies: 1 427
-- Name: link_artist_with_tag(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_artist_with_tag(_artist_id integer, _tag_id integer) RETURNS void
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $_$
BEGIN
	INSERT INTO mtl.artists_rel_tag (artist_id, tag_id) VALUES ($1, $2);
	UPDATE mtl.tags SET used = (used + 1) WHERE id = $2;
	RETURN;
EXCEPTION WHEN unique_violation THEN
	UPDATE mtl.artists_rel_tag SET power = (power+1) WHERE artist_id = $1 AND tag_id = $2;
	RETURN;
END;
$_$;


--
-- TOC entry 48 (class 1255 OID 1061410)
-- Dependencies: 427 1
-- Name: link_label_with_tag(text, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_label_with_tag(labelname text, tagname text) RETURNS void
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $_$
BEGIN
	
	INSERT INTO mtl.labels_rel_tag (label_id, tag_id) VALUES ((SELECT mtl.get_label_id_by_name($1)), (SELECT mtl.get_tag_id_by_name($2)));
	UPDATE mtl.tags SET used = (used + 1) WHERE id = (SELECT mtl.get_tag_id_by_name($2));
	RETURN;
EXCEPTION WHEN unique_violation THEN
	UPDATE mtl.labels_rel_tag SET power = (power+1) WHERE (label_id = (SELECT mtl.get_label_id_by_name($1)) AND tag_id = (SELECT mtl.get_tag_id_by_name($2)));
	RETURN;
END;
$_$;


--
-- TOC entry 71 (class 1255 OID 1122945)
-- Dependencies: 427 1
-- Name: link_label_with_tag(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_label_with_tag(_label_id integer, _tag_id integer) RETURNS void
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $_$
BEGIN
	INSERT INTO mtl.labels_rel_tag (label_id, tag_id) VALUES ($1, $2);
	UPDATE mtl.tags SET used = (used + 1) WHERE id = $2;
	RETURN;
EXCEPTION WHEN unique_violation THEN
	UPDATE mtl.labels_rel_tag SET power = (power+1) WHERE label_id = $1 AND tag_id = $2;
	RETURN;
END;
$_$;


--
-- TOC entry 49 (class 1255 OID 1066217)
-- Dependencies: 1 427
-- Name: link_member_with_role(text, text, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_member_with_role(membername text, rolename text, integer) RETURNS void
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $_$
BEGIN
	INSERT INTO mtl.members_rel_role (member_id, role_id, "year") VALUES ((SELECT mtl.get_member_id_by_name($1)), (SELECT mtl.get_role_id_by_name($2)), $3);
	RETURN;
EXCEPTION WHEN unique_violation THEN
	RETURN;
END;
$_$;


--
-- TOC entry 72 (class 1255 OID 1122946)
-- Dependencies: 1 427
-- Name: link_member_with_role(integer, integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_member_with_role(_member_id integer, _role_id integer, integer) RETURNS void
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $_$
BEGIN
	INSERT INTO mtl.members_rel_role (member_id, role_id, "year") VALUES ($1, $2, $3);
	RETURN;
EXCEPTION WHEN unique_violation THEN
	RETURN;
END;
$_$;


--
-- TOC entry 84 (class 1255 OID 1092142)
-- Dependencies: 1
-- Name: link_release_with_label(text, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_release_with_label(releasename text, labelname text) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE mtl.labels SET used = (used + 1) WHERE id = (SELECT mtl.get_label_id_by_name($2));
	INSERT INTO mtl.releases_rel_label (release_id, label_id) VALUES ((SELECT mtl.get_release_id_by_name($1)), (SELECT mtl.get_label_id_by_name($2)));
$_$;


--
-- TOC entry 73 (class 1255 OID 1122947)
-- Dependencies: 1
-- Name: link_release_with_label(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION link_release_with_label(_release_id integer, _label_id integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE mtl.labels SET used = (used + 1) WHERE id = $2;
	INSERT INTO mtl.releases_rel_label (release_id, label_id) VALUES ($1, $2);
$_$;


--
-- TOC entry 91 (class 1255 OID 1220593)
-- Dependencies: 1 427
-- Name: mark_leaked(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION mark_leaked(release_id integer) RETURNS void
    LANGUAGE plpgsql STRICT
    AS $_$DECLARE
	tmp record;
BEGIN
	UPDATE mtl.releases SET leaked = TRUE WHERE id = $1 AND NOT leaked;

	IF FOUND THEN
		FOR tmp IN SELECT user_id FROM mtl.watch_releases WHERE watch_releases.release_id = $1 LOOP
			INSERT INTO sys.tasks(user_id, task, param_int) VALUES (tmp.user_id, 'notify_leaked', $1);
		END LOOP;
	END IF;

	RETURN;
END;
$_$;


--
-- TOC entry 93 (class 1255 OID 1220594)
-- Dependencies: 427 1
-- Name: mark_unleaked(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION mark_unleaked(release_id integer) RETURNS void
    LANGUAGE plpgsql STRICT
    AS $_$DECLARE
	tmp record;
BEGIN
	UPDATE mtl.releases SET leaked = FALSE WHERE id = $1 AND leaked;

	IF FOUND THEN
		FOR tmp IN SELECT user_id FROM mtl.watch_releases WHERE watch_releases.release_id = $1 LOOP
			INSERT INTO sys.tasks(user_id, task, param_int) VALUES (tmp.user_id, 'notify_unleaked', $1);
		END LOOP;
	END IF;

	RETURN;
END;
$_$;


--
-- TOC entry 28 (class 1255 OID 1100022)
-- Dependencies: 1
-- Name: rename_release(text, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION rename_release(release_name text, new_name text) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE mtl.releases SET "name" = $2 WHERE "name" = $1;
$_$;


--
-- TOC entry 27 (class 1255 OID 1100011)
-- Dependencies: 1
-- Name: retr_artist_rel_release(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION retr_artist_rel_release(release_id integer) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT artist_id FROM mtl.artists_rel_release WHERE release_id = $1 LIMIT 1;
$_$;


--
-- TOC entry 52 (class 1255 OID 1101109)
-- Dependencies: 1
-- Name: retr_tag_rel_artist(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION retr_tag_rel_artist(artist_id integer) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT tag_id FROM mtl.artists_rel_tag WHERE artist_id = $1 ORDER BY power DESC LIMIT 1;
$_$;


--
-- TOC entry 33 (class 1255 OID 1085415)
-- Dependencies: 1
-- Name: retr_tag_rel_release(integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION retr_tag_rel_release(release_id integer) RETURNS integer
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT tag_id FROM mtl.releases_rel_tag WHERE release_id = $1 ORDER BY power DESC LIMIT 1;
$_$;


--
-- TOC entry 41 (class 1255 OID 1100420)
-- Dependencies: 427 1
-- Name: set_artist_wiki(integer, integer, text, text); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION set_artist_wiki(art_id integer, user_id integer, wiki text, src text) RETURNS void
    LANGUAGE plpgsql SECURITY DEFINER
    AS $_$
BEGIN
	INSERT INTO mtl.artists_wiki(artist_id, added_by_user_id, data, source) VALUES ($1, $2, $3, $4);
	RETURN;
EXCEPTION WHEN unique_violation THEN
	UPDATE mtl.artists_wiki SET changed_by_user_id = $2, data = $3, source = $4 WHERE artist_id = $1;
	RETURN;
END;
$_$;


--
-- TOC entry 78 (class 1255 OID 1123512)
-- Dependencies: 1
-- Name: unlink_artist_with_label(integer, integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION unlink_artist_with_label(_artist_id integer, _label_id integer, _year integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	DELETE FROM mtl.artists_rel_label WHERE artist_id = $1 AND label_id = $2 AND year = $3;
	UPDATE mtl.labels SET used = (used - 1) WHERE id = $2;
	DELETE FROM mtl.labels WHERE id = $2 AND used <= 0;
  $_$;


--
-- TOC entry 77 (class 1255 OID 1123506)
-- Dependencies: 1
-- Name: unlink_artist_with_tag(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION unlink_artist_with_tag(_artist_id integer, _tag_id integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	DELETE FROM mtl.artists_rel_tag WHERE artist_id = $1 AND tag_id = $2;
	UPDATE mtl.tags SET used = (used - 1) WHERE id = $2;
	DELETE FROM mtl.tags WHERE id = $2 AND used <= 0;
  $_$;


--
-- TOC entry 57 (class 1255 OID 1103039)
-- Dependencies: 1
-- Name: unwatch_artist(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION unwatch_artist(integer, integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	DELETE FROM mtl.watch_artists WHERE user_id = $1 AND artist_id = $2;
$_$;


--
-- TOC entry 55 (class 1255 OID 1103037)
-- Dependencies: 1
-- Name: unwatch_release(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION unwatch_release(integer, integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	DELETE FROM mtl.watch_releases WHERE user_id = $1 AND release_id = $2;
$_$;


--
-- TOC entry 94 (class 1255 OID 1101183)
-- Dependencies: 427 1
-- Name: update_schedules(); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION update_schedules() RETURNS void
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$DECLARE
	tmp record;
	tmp2 record;
BEGIN
	FOR tmp IN SELECT id, schedule_date FROM mtl.releases WHERE mtl.is_schedule_date_ymd(schedule_date) LOOP
		CONTINUE WHEN tmp.schedule_date::date > CURRENT_DATE;
		UPDATE mtl.releases SET release_date = schedule_date::date, schedule_date = NULL, year_finished = EXTRACT (year FROM schedule_date::date), leaked = TRUE WHERE id = tmp.id;
		FOR tmp2 IN SELECT user_id FROM mtl.watch_releases WHERE release_id = tmp.id LOOP
			INSERT INTO sys.tasks (user_id, task, param_int) VALUES (tmp2.user_id, 'notify_release', tmp.id);
		END LOOP;

		FOR tmp2 IN SELECT w.user_id, w.artist_id FROM mtl.watch_artists w LEFT JOIN mtl.artists_rel_release rel ON (w.artist_id = rel.artist_id) LEFT JOIN mtl.releases r ON (r.id = rel.release_id) WHERE  r.id = tmp.id LOOP
			INSERT INTO sys.tasks (user_id, task, param_int) VALUES (tmp2.user_id, 'notify_artist', tmp2.artist_id);
		END LOOP;
	END LOOP;
	RETURN;
END;
$$;


--
-- TOC entry 56 (class 1255 OID 1103038)
-- Dependencies: 1
-- Name: watch_artist(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION watch_artist(integer, integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	INSERT INTO mtl.watch_artists (user_id, artist_id) VALUES ($1, $2);
$_$;


--
-- TOC entry 58 (class 1255 OID 1103308)
-- Dependencies: 1 427
-- Name: watch_release(integer, integer); Type: FUNCTION; Schema: mtl; Owner: -
--

CREATE FUNCTION watch_release(integer, integer) RETURNS void
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $_$
BEGIN
	INSERT INTO mtl.watch_releases (user_id, release_id) VALUES ($1, $2);
	RETURN;
EXCEPTION WHEN unique_violation THEN
	RETURN;
END;
	
$_$;


SET search_path = public, pg_catalog;

--
-- TOC entry 92 (class 1255 OID 1099881)
-- Dependencies: 427 6
-- Name: add_artist_stats_row(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION add_artist_stats_row() RETURNS "trigger"
    LANGUAGE plpgsql STRICT SECURITY DEFINER
    AS $$
BEGIN
	INSERT INTO mtl.artists_stats (artist_id) SELECT id FROM mtl.artists ORDER BY id DESC LIMIT 1;
END;
$$;


SET search_path = sys, pg_catalog;

--
-- TOC entry 38 (class 1255 OID 1100164)
-- Dependencies: 3
-- Name: add_news(integer, text, text); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION add_news(integer, text, text) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	INSERT INTO sys.news (added_by_user_id, caption, message) VALUES ($1, $2, $3);
$_$;


--
-- TOC entry 82 (class 1255 OID 1084534)
-- Dependencies: 3
-- Name: add_todo(text); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION add_todo(text) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	INSERT INTO sys.todo (message) VALUES ($1);
$_$;


--
-- TOC entry 79 (class 1255 OID 1127334)
-- Dependencies: 3
-- Name: add_userpic(integer, character varying); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION add_userpic(_user_id integer, _image_name character varying) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	INSERT INTO sys.userpics (user_id, image_name) VALUES ($1, $2);
$_$;


--
-- TOC entry 83 (class 1255 OID 1084535)
-- Dependencies: 3
-- Name: close_todo(integer); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION close_todo(integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE sys.todo SET done = 't' WHERE id = $1;
$_$;


--
-- TOC entry 21 (class 1255 OID 1192707)
-- Dependencies: 3
-- Name: delete_todo(integer); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION delete_todo(integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE sys.todo SET deleted = 't' WHERE id = $1;
$_$;


--
-- TOC entry 80 (class 1255 OID 1127335)
-- Dependencies: 3
-- Name: delete_userpic(integer); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION delete_userpic(_user_id integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	DELETE FROM sys.userpics WHERE user_id = $1;
$_$;


--
-- TOC entry 39 (class 1255 OID 1100166)
-- Dependencies: 3
-- Name: get_user_name_by_id(integer); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION get_user_name_by_id(integer) RETURNS character
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT "login" FROM sys.users WHERE id = $1;
$_$;


--
-- TOC entry 86 (class 1255 OID 1198374)
-- Dependencies: 3 427
-- Name: lastfm_cache_result(character varying, character varying, text); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION lastfm_cache_result(_method character varying, _params character varying, _result text) RETURNS void
    LANGUAGE plpgsql
    AS $$BEGIN

INSERT INTO sys.lastfm_cache (method, params, result) VALUES (_method, _params, _result);

EXCEPTION WHEN unique_violation THEN

UPDATE sys.lastfm_cache SET result=_result, time=NOW() WHERE method=_method AND params=_params;

END;$$;


--
-- TOC entry 26 (class 1255 OID 1085300)
-- Dependencies: 3
-- Name: register_user(text, text, text, cidr); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION register_user(text, text, text, cidr) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	INSERT INTO sys.users ("login", "password", email, registration_ip) VALUES ($1, $2, $3, $4);
$_$;


--
-- TOC entry 24 (class 1255 OID 1085264)
-- Dependencies: 3
-- Name: reopen_todo(integer); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION reopen_todo(integer) RETURNS void
    LANGUAGE sql STRICT SECURITY DEFINER
    AS $_$
	UPDATE sys.todo SET done = 'f' WHERE id = $1;
$_$;


--
-- TOC entry 81 (class 1255 OID 1127337)
-- Dependencies: 3
-- Name: user_has_userpic(integer); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION user_has_userpic(_user_id integer) RETURNS character
    LANGUAGE sql IMMUTABLE STRICT SECURITY DEFINER
    AS $_$
	SELECT image_name FROM sys.userpics WHERE user_id = $1;
$_$;


--
-- TOC entry 51 (class 1255 OID 1100783)
-- Dependencies: 3
-- Name: write_log(integer, integer, text, text); Type: FUNCTION; Schema: sys; Owner: -
--

CREATE FUNCTION write_log(integer, integer, text, text) RETURNS void
    LANGUAGE sql SECURITY DEFINER
    AS $_$
	INSERT INTO sys.log (user_id, action_id, param1, param2) VALUES ($1, $2, $3, $4);
$_$;


SET search_path = mtl, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- TOC entry 1465 (class 1259 OID 1085309)
-- Dependencies: 1879 1880 1881 1882 1
-- Name: artists; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists (
    id integer NOT NULL,
    name character(128) NOT NULL,
    country character(64),
    year_formed integer,
    year_disbanded integer,
    solo boolean DEFAULT false NOT NULL,
    homepage character(128),
    signed boolean DEFAULT true NOT NULL,
    old_name character(128),
    reserved_name character(128),
    myspace character(128),
    has_picture boolean DEFAULT false NOT NULL,
    picture_path character(140),
    CONSTRAINT artists_name_check CHECK ((name <> ''::bpchar))
);


--
-- TOC entry 1480 (class 1259 OID 1100500)
-- Dependencies: 1902 1903 1
-- Name: artists_comments; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists_comments (
    id integer NOT NULL,
    artist_id integer NOT NULL,
    date_added timestamp with time zone DEFAULT now(),
    added_by_user_id integer DEFAULT 0,
    message character(1024) NOT NULL,
    ip cidr
);


--
-- TOC entry 1479 (class 1259 OID 1100498)
-- Dependencies: 1480 1
-- Name: artists_comments_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_comments_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2127 (class 0 OID 0)
-- Dependencies: 1479
-- Name: artists_comments_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_comments_id_seq OWNED BY artists_comments.id;


--
-- TOC entry 1464 (class 1259 OID 1085307)
-- Dependencies: 1 1465
-- Name: artists_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2128 (class 0 OID 0)
-- Dependencies: 1464
-- Name: artists_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_id_seq OWNED BY artists.id;


--
-- TOC entry 1439 (class 1259 OID 1077344)
-- Dependencies: 1
-- Name: artists_rel_label; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists_rel_label (
    id integer NOT NULL,
    artist_id integer NOT NULL,
    label_id integer NOT NULL,
    year integer NOT NULL
);


--
-- TOC entry 1438 (class 1259 OID 1077342)
-- Dependencies: 1439 1
-- Name: artists_rel_label_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_rel_label_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2129 (class 0 OID 0)
-- Dependencies: 1438
-- Name: artists_rel_label_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_rel_label_id_seq OWNED BY artists_rel_label.id;


--
-- TOC entry 1437 (class 1259 OID 1077337)
-- Dependencies: 1
-- Name: artists_rel_member; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists_rel_member (
    id integer NOT NULL,
    artist_id integer NOT NULL,
    member_id integer NOT NULL,
    year_started integer NOT NULL,
    year_finished integer
);


--
-- TOC entry 1436 (class 1259 OID 1077335)
-- Dependencies: 1437 1
-- Name: artists_rel_member_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_rel_member_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2130 (class 0 OID 0)
-- Dependencies: 1436
-- Name: artists_rel_member_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_rel_member_id_seq OWNED BY artists_rel_member.id;


--
-- TOC entry 1433 (class 1259 OID 1077322)
-- Dependencies: 1
-- Name: artists_rel_release; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists_rel_release (
    id integer NOT NULL,
    artist_id integer NOT NULL,
    release_id integer NOT NULL
);


--
-- TOC entry 1432 (class 1259 OID 1077320)
-- Dependencies: 1433 1
-- Name: artists_rel_release_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_rel_release_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2131 (class 0 OID 0)
-- Dependencies: 1432
-- Name: artists_rel_release_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_rel_release_id_seq OWNED BY artists_rel_release.id;


--
-- TOC entry 1435 (class 1259 OID 1077329)
-- Dependencies: 1844 1
-- Name: artists_rel_tag; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists_rel_tag (
    id integer NOT NULL,
    artist_id integer NOT NULL,
    tag_id integer NOT NULL,
    power integer DEFAULT 1
);


--
-- TOC entry 1434 (class 1259 OID 1077327)
-- Dependencies: 1435 1
-- Name: artists_rel_tag_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_rel_tag_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2132 (class 0 OID 0)
-- Dependencies: 1434
-- Name: artists_rel_tag_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_rel_tag_id_seq OWNED BY artists_rel_tag.id;


--
-- TOC entry 1471 (class 1259 OID 1099867)
-- Dependencies: 1886 1887 1888 1890 1
-- Name: artists_stats; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists_stats (
    id integer NOT NULL,
    artist_id integer NOT NULL,
    date_added timestamp without time zone DEFAULT now(),
    date_changed timestamp without time zone,
    added_by_user_id integer,
    changed_by_user_id integer,
    rank_counter integer DEFAULT 0,
    visited_counter integer DEFAULT 0,
    used_counter integer DEFAULT 0
);


--
-- TOC entry 1470 (class 1259 OID 1099865)
-- Dependencies: 1471 1
-- Name: artists_stats_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_stats_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2133 (class 0 OID 0)
-- Dependencies: 1470
-- Name: artists_stats_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_stats_id_seq OWNED BY artists_stats.id;


--
-- TOC entry 1499 (class 1259 OID 1199360)
-- Dependencies: 1582 1
-- Name: artists_view; Type: VIEW; Schema: mtl; Owner: -
--

CREATE VIEW artists_view AS
    SELECT artists.id, artists.name, artists.old_name, artists.country, artists.myspace, artists.signed, artists.has_picture, artists.picture_path, artists.year_formed, artists.year_disbanded, artists.homepage, (SELECT get_label_name_by_id(artists_rel_label.label_id) AS get_label_name_by_id FROM artists_rel_label WHERE (artists_rel_label.artist_id = artists.id) ORDER BY artists_rel_label."year" DESC LIMIT 1) AS label, (SELECT get_tag_name_by_id(artists_rel_tag.tag_id) AS get_tag_name_by_id FROM artists_rel_tag WHERE (artists_rel_tag.artist_id = artists.id) ORDER BY artists_rel_tag.power DESC LIMIT 1) AS tag, (SELECT get_release_name_by_id(artists_rel_release.release_id) AS get_release_name_by_id FROM artists_rel_release WHERE ((artists_rel_release.artist_id = artists.id) AND (NOT is_scheduled_release(artists_rel_release.release_id))) ORDER BY get_release_year_by_id(artists_rel_release.release_id) DESC LIMIT 1) AS latest_album, (SELECT get_release_year_by_id(artists_rel_release.release_id) AS get_release_year_by_id FROM artists_rel_release WHERE ((artists_rel_release.artist_id = artists.id) AND (NOT is_scheduled_release(artists_rel_release.release_id))) ORDER BY get_release_year_by_id(artists_rel_release.release_id) DESC LIMIT 1) AS latest_album_year, (SELECT get_release_count_by_artist_id(artists.id) AS get_release_count_by_artist_id FROM artists_rel_release LIMIT 1) AS release_count FROM artists;


--
-- TOC entry 1478 (class 1259 OID 1100408)
-- Dependencies: 1899 1900 1
-- Name: artists_wiki; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE artists_wiki (
    id integer NOT NULL,
    artist_id integer NOT NULL,
    date_added timestamp without time zone DEFAULT now(),
    date_changed timestamp without time zone,
    added_by_user_id integer DEFAULT 0,
    changed_by_user_id integer,
    source character(64),
    data character(4096)
);


--
-- TOC entry 1477 (class 1259 OID 1100406)
-- Dependencies: 1478 1
-- Name: artists_wiki_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE artists_wiki_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2134 (class 0 OID 0)
-- Dependencies: 1477
-- Name: artists_wiki_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE artists_wiki_id_seq OWNED BY artists_wiki.id;


--
-- TOC entry 1459 (class 1259 OID 1077438)
-- Dependencies: 1
-- Name: gear; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE gear (
    id integer NOT NULL,
    name character(128) NOT NULL,
    gear_type_id integer,
    description text,
    source text,
    img text
);


--
-- TOC entry 1458 (class 1259 OID 1077436)
-- Dependencies: 1 1459
-- Name: gear_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE gear_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2135 (class 0 OID 0)
-- Dependencies: 1458
-- Name: gear_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE gear_id_seq OWNED BY gear.id;


--
-- TOC entry 1453 (class 1259 OID 1077410)
-- Dependencies: 1862 1863 1
-- Name: labels; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE labels (
    id integer NOT NULL,
    name character(128) NOT NULL,
    used integer DEFAULT 0,
    CONSTRAINT labels_name_check CHECK ((name <> ''::bpchar))
);


--
-- TOC entry 1452 (class 1259 OID 1077408)
-- Dependencies: 1 1453
-- Name: labels_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE labels_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2136 (class 0 OID 0)
-- Dependencies: 1452
-- Name: labels_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE labels_id_seq OWNED BY labels.id;


--
-- TOC entry 1441 (class 1259 OID 1077351)
-- Dependencies: 1848 1
-- Name: labels_rel_tag; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE labels_rel_tag (
    id integer NOT NULL,
    label_id integer NOT NULL,
    tag_id integer NOT NULL,
    power integer DEFAULT 1
);


--
-- TOC entry 1440 (class 1259 OID 1077349)
-- Dependencies: 1 1441
-- Name: labels_rel_tag_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE labels_rel_tag_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2137 (class 0 OID 0)
-- Dependencies: 1440
-- Name: labels_rel_tag_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE labels_rel_tag_id_seq OWNED BY labels_rel_tag.id;


--
-- TOC entry 1455 (class 1259 OID 1077419)
-- Dependencies: 1865 1
-- Name: members; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE members (
    id integer NOT NULL,
    name character(128) NOT NULL,
    used integer DEFAULT 0
);


--
-- TOC entry 1454 (class 1259 OID 1077417)
-- Dependencies: 1 1455
-- Name: members_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE members_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2138 (class 0 OID 0)
-- Dependencies: 1454
-- Name: members_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE members_id_seq OWNED BY members.id;


--
-- TOC entry 1445 (class 1259 OID 1077366)
-- Dependencies: 1
-- Name: members_rel_gear; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE members_rel_gear (
    id integer NOT NULL,
    member_id integer NOT NULL,
    gear_id integer NOT NULL
);


--
-- TOC entry 1444 (class 1259 OID 1077364)
-- Dependencies: 1 1445
-- Name: members_rel_gear_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE members_rel_gear_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2139 (class 0 OID 0)
-- Dependencies: 1444
-- Name: members_rel_gear_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE members_rel_gear_id_seq OWNED BY members_rel_gear.id;


--
-- TOC entry 1443 (class 1259 OID 1077359)
-- Dependencies: 1
-- Name: members_rel_role; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE members_rel_role (
    id integer NOT NULL,
    member_id integer NOT NULL,
    role_id integer NOT NULL,
    year integer NOT NULL
);


--
-- TOC entry 1442 (class 1259 OID 1077357)
-- Dependencies: 1443 1
-- Name: members_rel_role_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE members_rel_role_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2140 (class 0 OID 0)
-- Dependencies: 1442
-- Name: members_rel_role_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE members_rel_role_id_seq OWNED BY members_rel_role.id;


--
-- TOC entry 1469 (class 1259 OID 1085383)
-- Dependencies: 1885 1
-- Name: record_types; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE record_types (
    id integer NOT NULL,
    name character(64),
    description character(512),
    priority integer DEFAULT 1
);


--
-- TOC entry 1468 (class 1259 OID 1085381)
-- Dependencies: 1 1469
-- Name: record_types_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE record_types_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2141 (class 0 OID 0)
-- Dependencies: 1468
-- Name: record_types_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE record_types_id_seq OWNED BY record_types.id;


--
-- TOC entry 1449 (class 1259 OID 1077394)
-- Dependencies: 1853 1854 1855 1857 1858 1
-- Name: releases; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE releases (
    id integer NOT NULL,
    name character(128) NOT NULL,
    year_started integer,
    year_finished integer,
    release_date date,
    track_count integer,
    record_type_id integer DEFAULT 11,
    length integer,
    schedule_date character(32),
    tba boolean DEFAULT false NOT NULL,
    has_picture boolean DEFAULT false NOT NULL,
    picture_path character(140),
    leaked boolean DEFAULT false,
    CONSTRAINT releases_name_check CHECK ((name <> ''::bpchar))
);


--
-- TOC entry 1490 (class 1259 OID 1125519)
-- Dependencies: 1911 1912 1
-- Name: releases_comments; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE releases_comments (
    id integer NOT NULL,
    release_id integer NOT NULL,
    date_added timestamp with time zone DEFAULT now(),
    added_by_user_id integer DEFAULT 0,
    message character(1024) NOT NULL,
    ip cidr
);


--
-- TOC entry 1489 (class 1259 OID 1125517)
-- Dependencies: 1 1490
-- Name: releases_comments_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE releases_comments_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2142 (class 0 OID 0)
-- Dependencies: 1489
-- Name: releases_comments_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE releases_comments_id_seq OWNED BY releases_comments.id;


--
-- TOC entry 1448 (class 1259 OID 1077392)
-- Dependencies: 1449 1
-- Name: releases_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE releases_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2143 (class 0 OID 0)
-- Dependencies: 1448
-- Name: releases_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE releases_id_seq OWNED BY releases.id;


--
-- TOC entry 1467 (class 1259 OID 1085374)
-- Dependencies: 1
-- Name: releases_rel_label; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE releases_rel_label (
    id integer NOT NULL,
    release_id integer NOT NULL,
    label_id integer NOT NULL
);


--
-- TOC entry 1466 (class 1259 OID 1085372)
-- Dependencies: 1467 1
-- Name: releases_rel_label_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE releases_rel_label_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2144 (class 0 OID 0)
-- Dependencies: 1466
-- Name: releases_rel_label_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE releases_rel_label_id_seq OWNED BY releases_rel_label.id;


--
-- TOC entry 1447 (class 1259 OID 1077373)
-- Dependencies: 1852 1
-- Name: releases_rel_tag; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE releases_rel_tag (
    id integer NOT NULL,
    release_id integer NOT NULL,
    tag_id integer NOT NULL,
    power integer DEFAULT 1
);


--
-- TOC entry 1446 (class 1259 OID 1077371)
-- Dependencies: 1 1447
-- Name: releases_rel_tag_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE releases_rel_tag_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2145 (class 0 OID 0)
-- Dependencies: 1446
-- Name: releases_rel_tag_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE releases_rel_tag_id_seq OWNED BY releases_rel_tag.id;


--
-- TOC entry 1473 (class 1259 OID 1099953)
-- Dependencies: 1892 1893 1894 1895 1
-- Name: releases_stats; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE releases_stats (
    id integer NOT NULL,
    release_id integer NOT NULL,
    date_added timestamp without time zone DEFAULT now(),
    date_changed timestamp without time zone,
    added_by_user_id integer,
    changed_by_user_id integer,
    rank_counter integer DEFAULT 0,
    visited_counter integer DEFAULT 0,
    used_counter integer DEFAULT 0
);


--
-- TOC entry 1472 (class 1259 OID 1099951)
-- Dependencies: 1 1473
-- Name: releases_stats_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE releases_stats_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2146 (class 0 OID 0)
-- Dependencies: 1472
-- Name: releases_stats_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE releases_stats_id_seq OWNED BY releases_stats.id;


--
-- TOC entry 1506 (class 1259 OID 1227843)
-- Dependencies: 1585 1
-- Name: releases_view; Type: VIEW; Schema: mtl; Owner: -
--

CREATE VIEW releases_view AS
    SELECT releases.id, releases.name, releases.tba, releases.release_date, releases.schedule_date, releases.leaked, get_artist_name_by_id(get_artist_id_by_release_id(releases.id)) AS artist_name, get_label_name_by_id((SELECT releases_rel_label.label_id FROM releases_rel_label WHERE (releases_rel_label.release_id = releases.id) LIMIT 1)) AS label_name, get_tag_name_by_id(retr_tag_rel_release(releases.id)) AS tag, releases.year_finished, releases.track_count, releases.length, get_record_type_name_by_id(releases.record_type_id) AS record_type, get_tag_name_by_id(retr_tag_rel_artist(get_artist_id_by_release_id(releases.id))) AS genre, releases.has_picture FROM releases;


--
-- TOC entry 1488 (class 1259 OID 1125486)
-- Dependencies: 1908 1909 1
-- Name: releases_wiki; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE releases_wiki (
    id integer NOT NULL,
    release_id integer NOT NULL,
    date_added timestamp without time zone DEFAULT now(),
    date_changed timestamp without time zone,
    added_by_user_id integer DEFAULT 0,
    changed_by_user_id integer,
    source character(64),
    data character(4096)
);


--
-- TOC entry 1487 (class 1259 OID 1125484)
-- Dependencies: 1488 1
-- Name: releases_wiki_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE releases_wiki_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2147 (class 0 OID 0)
-- Dependencies: 1487
-- Name: releases_wiki_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE releases_wiki_id_seq OWNED BY releases_wiki.id;


--
-- TOC entry 1457 (class 1259 OID 1077428)
-- Dependencies: 1
-- Name: roles; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE roles (
    id integer NOT NULL,
    name character(64) NOT NULL,
    description text
);


--
-- TOC entry 1456 (class 1259 OID 1077426)
-- Dependencies: 1 1457
-- Name: roles_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE roles_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2148 (class 0 OID 0)
-- Dependencies: 1456
-- Name: roles_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE roles_id_seq OWNED BY roles.id;


--
-- TOC entry 1451 (class 1259 OID 1077401)
-- Dependencies: 1860 1
-- Name: tags; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE tags (
    id integer NOT NULL,
    name character(64) NOT NULL,
    used integer DEFAULT 0
);


--
-- TOC entry 1500 (class 1259 OID 1220590)
-- Dependencies: 1583 1
-- Name: schedules_view; Type: VIEW; Schema: mtl; Owner: -
--

CREATE VIEW schedules_view AS
    SELECT releases.id, releases.name, get_record_type_name_by_id(releases.record_type_id) AS record_type, get_artist_name_by_id(get_artist_id_by_release_id(releases.id)) AS artist_name, get_label_name_by_id((SELECT releases_rel_label.label_id FROM releases_rel_label WHERE (releases_rel_label.release_id = releases.id) LIMIT 1)) AS label_name, releases.schedule_date, releases.tba, (array_to_string(ARRAY(SELECT btrim((tag.name)::text) AS btrim FROM (artists_rel_tag rel LEFT JOIN tags tag ON ((tag.id = rel.tag_id))) WHERE (rel.artist_id = get_artist_id_by_release_id(releases.id)) ORDER BY rel.power DESC LIMIT 5), ','::text))::bpchar AS genre, releases.leaked FROM releases WHERE is_scheduled_release(releases.id);


--
-- TOC entry 1450 (class 1259 OID 1077399)
-- Dependencies: 1 1451
-- Name: tags_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE tags_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2149 (class 0 OID 0)
-- Dependencies: 1450
-- Name: tags_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE tags_id_seq OWNED BY tags.id;


--
-- TOC entry 1474 (class 1259 OID 1099977)
-- Dependencies: 1
-- Name: tba_release_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE tba_release_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 1485 (class 1259 OID 1103017)
-- Dependencies: 1
-- Name: watch_artists; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE watch_artists (
    id integer NOT NULL,
    user_id integer NOT NULL,
    artist_id integer NOT NULL
);


--
-- TOC entry 1484 (class 1259 OID 1103015)
-- Dependencies: 1485 1
-- Name: watch_artists_id_seq; Type: SEQUENCE; Schema: mtl; Owner: -
--

CREATE SEQUENCE watch_artists_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2150 (class 0 OID 0)
-- Dependencies: 1484
-- Name: watch_artists_id_seq; Type: SEQUENCE OWNED BY; Schema: mtl; Owner: -
--

ALTER SEQUENCE watch_artists_id_seq OWNED BY watch_artists.id;


--
-- TOC entry 1503 (class 1259 OID 1220602)
-- Dependencies: 1584 1
-- Name: watch_artists_view; Type: VIEW; Schema: mtl; Owner: -
--

CREATE VIEW watch_artists_view AS
    SELECT w.user_id, a.id, a.name, (array_to_string(ARRAY(SELECT btrim((tag.name)::text) AS btrim FROM (artists_rel_tag rel LEFT JOIN tags tag ON ((tag.id = rel.tag_id))) WHERE (rel.artist_id = a.id) ORDER BY rel.power DESC LIMIT 5), ','::text))::bpchar AS genre, get_artist_schedule_info(w.artist_id) AS schedule_info FROM (watch_artists w LEFT JOIN artists a ON ((w.artist_id = a.id)));


--
-- TOC entry 1483 (class 1259 OID 1102996)
-- Dependencies: 1
-- Name: watch_releases; Type: TABLE; Schema: mtl; Owner: -; Tablespace: 
--

CREATE TABLE watch_releases (
    user_id integer NOT NULL,
    release_id integer NOT NULL
);


--
-- TOC entry 1486 (class 1259 OID 1103376)
-- Dependencies: 1581 1
-- Name: watch_releases_view; Type: VIEW; Schema: mtl; Owner: -
--

CREATE VIEW watch_releases_view AS
    SELECT releases.id, releases.name, releases.schedule_date, get_artist_name_by_id(get_artist_id_by_release_id(releases.id)) AS artist_name, (array_to_string(ARRAY(SELECT btrim((tag.name)::text) AS btrim FROM (artists_rel_tag rel LEFT JOIN tags tag ON ((tag.id = rel.tag_id))) WHERE (rel.artist_id = get_artist_id_by_release_id(releases.id)) ORDER BY rel.power DESC LIMIT 5), ','::text))::bpchar AS genre, get_record_type_name_by_id(releases.record_type_id) AS record_type, releases.tba, watch_releases.user_id FROM releases, watch_releases WHERE (releases.id = watch_releases.release_id);


SET search_path = sys, pg_catalog;

--
-- TOC entry 1505 (class 1259 OID 1220608)
-- Dependencies: 3
-- Name: geoip; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE geoip (
    id integer NOT NULL,
    net cidr NOT NULL,
    country character varying(2)
);


--
-- TOC entry 1504 (class 1259 OID 1220606)
-- Dependencies: 3 1505
-- Name: geoip_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE geoip_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2151 (class 0 OID 0)
-- Dependencies: 1504
-- Name: geoip_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE geoip_id_seq OWNED BY geoip.id;


--
-- TOC entry 1496 (class 1259 OID 1198367)
-- Dependencies: 1917 3
-- Name: lastfm_cache; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE lastfm_cache (
    id integer NOT NULL,
    method character varying(64) NOT NULL,
    params character varying(512) NOT NULL,
    result text NOT NULL,
    "time" timestamp without time zone DEFAULT now() NOT NULL
);


--
-- TOC entry 1482 (class 1259 OID 1100787)
-- Dependencies: 1905 3
-- Name: log; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE log (
    id integer NOT NULL,
    action_id integer NOT NULL,
    user_id integer,
    "timestamp" timestamp without time zone DEFAULT now(),
    param1 character(128),
    param2 character(128)
);


--
-- TOC entry 1481 (class 1259 OID 1100785)
-- Dependencies: 1482 3
-- Name: log_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE log_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2152 (class 0 OID 0)
-- Dependencies: 1481
-- Name: log_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE log_id_seq OWNED BY log.id;


--
-- TOC entry 1498 (class 1259 OID 1199014)
-- Dependencies: 1919 3
-- Name: mail_log; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE mail_log (
    id integer NOT NULL,
    "to" character(128),
    subject character(128),
    message text,
    "from" character(128),
    result boolean,
    section character(16),
    "time" timestamp without time zone DEFAULT now()
);


--
-- TOC entry 1497 (class 1259 OID 1199012)
-- Dependencies: 1498 3
-- Name: mail_log_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE mail_log_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2153 (class 0 OID 0)
-- Dependencies: 1497
-- Name: mail_log_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE mail_log_id_seq OWNED BY mail_log.id;


--
-- TOC entry 1476 (class 1259 OID 1100155)
-- Dependencies: 1897 3
-- Name: news; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE news (
    id integer NOT NULL,
    added_by_user_id integer,
    date timestamp with time zone DEFAULT now() NOT NULL,
    caption text NOT NULL,
    message text NOT NULL,
    pic_related text
);


--
-- TOC entry 1475 (class 1259 OID 1100153)
-- Dependencies: 3 1476
-- Name: news_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE news_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2154 (class 0 OID 0)
-- Dependencies: 1475
-- Name: news_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE news_id_seq OWNED BY news.id;


--
-- TOC entry 1495 (class 1259 OID 1198365)
-- Dependencies: 1496 3
-- Name: sys.lastfm_cache_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE "sys.lastfm_cache_id_seq"
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2155 (class 0 OID 0)
-- Dependencies: 1495
-- Name: sys.lastfm_cache_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE "sys.lastfm_cache_id_seq" OWNED BY lastfm_cache.id;


--
-- TOC entry 1502 (class 1259 OID 1220597)
-- Dependencies: 1921 1922 3
-- Name: tasks; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE tasks (
    id integer NOT NULL,
    user_id integer,
    task character varying(32),
    param_int integer,
    param_str character varying(256),
    completed boolean DEFAULT false,
    time_added timestamp without time zone DEFAULT now(),
    time_completed timestamp without time zone
);


--
-- TOC entry 1501 (class 1259 OID 1220595)
-- Dependencies: 1502 3
-- Name: tasks_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE tasks_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2156 (class 0 OID 0)
-- Dependencies: 1501
-- Name: tasks_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE tasks_id_seq OWNED BY tasks.id;


--
-- TOC entry 1461 (class 1259 OID 1085210)
-- Dependencies: 1868 1870 3
-- Name: todo; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE todo (
    id integer NOT NULL,
    message text NOT NULL,
    done boolean DEFAULT false NOT NULL,
    deleted boolean DEFAULT false
);


--
-- TOC entry 1460 (class 1259 OID 1085208)
-- Dependencies: 1461 3
-- Name: todo_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE todo_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2157 (class 0 OID 0)
-- Dependencies: 1460
-- Name: todo_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE todo_id_seq OWNED BY todo.id;


--
-- TOC entry 1492 (class 1259 OID 1127325)
-- Dependencies: 3
-- Name: userpics; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE userpics (
    id integer NOT NULL,
    user_id integer NOT NULL,
    image_name character(64) NOT NULL
);


--
-- TOC entry 1491 (class 1259 OID 1127323)
-- Dependencies: 1492 3
-- Name: userpics_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE userpics_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2158 (class 0 OID 0)
-- Dependencies: 1491
-- Name: userpics_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE userpics_id_seq OWNED BY userpics.id;


--
-- TOC entry 1463 (class 1259 OID 1085285)
-- Dependencies: 1871 1872 1873 1875 1876 1877 3
-- Name: users; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE users (
    id integer NOT NULL,
    password character(32) NOT NULL,
    email character(128) NOT NULL,
    admin boolean DEFAULT false NOT NULL,
    banned boolean DEFAULT false NOT NULL,
    ban_reason character(128),
    registration_date timestamp without time zone DEFAULT now() NOT NULL,
    registration_ip inet,
    last_login timestamp with time zone,
    access smallint DEFAULT 1 NOT NULL,
    cookie character(32),
    name character(32) NOT NULL,
    picture_path character(128),
    lang character varying(2) DEFAULT 'en'::character varying NOT NULL,
    select_lang boolean DEFAULT false NOT NULL
);


--
-- TOC entry 1462 (class 1259 OID 1085283)
-- Dependencies: 3 1463
-- Name: users_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE users_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2159 (class 0 OID 0)
-- Dependencies: 1462
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE users_id_seq OWNED BY users.id;


--
-- TOC entry 1494 (class 1259 OID 1192168)
-- Dependencies: 1915 3
-- Name: visits; Type: TABLE; Schema: sys; Owner: -; Tablespace: 
--

CREATE TABLE visits (
    id integer NOT NULL,
    "timestamp" timestamp without time zone DEFAULT now(),
    ip inet,
    referer character varying,
    page character varying,
    user_id integer
);


--
-- TOC entry 1493 (class 1259 OID 1192166)
-- Dependencies: 3 1494
-- Name: visits_id_seq; Type: SEQUENCE; Schema: sys; Owner: -
--

CREATE SEQUENCE visits_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- TOC entry 2160 (class 0 OID 0)
-- Dependencies: 1493
-- Name: visits_id_seq; Type: SEQUENCE OWNED BY; Schema: sys; Owner: -
--

ALTER SEQUENCE visits_id_seq OWNED BY visits.id;


SET search_path = mtl, pg_catalog;

--
-- TOC entry 1878 (class 2604 OID 1085311)
-- Dependencies: 1464 1465 1465
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists ALTER COLUMN id SET DEFAULT nextval('artists_id_seq'::regclass);


--
-- TOC entry 1901 (class 2604 OID 1100502)
-- Dependencies: 1480 1479 1480
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists_comments ALTER COLUMN id SET DEFAULT nextval('artists_comments_id_seq'::regclass);


--
-- TOC entry 1846 (class 2604 OID 1077346)
-- Dependencies: 1439 1438 1439
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists_rel_label ALTER COLUMN id SET DEFAULT nextval('artists_rel_label_id_seq'::regclass);


--
-- TOC entry 1845 (class 2604 OID 1077339)
-- Dependencies: 1437 1436 1437
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists_rel_member ALTER COLUMN id SET DEFAULT nextval('artists_rel_member_id_seq'::regclass);


--
-- TOC entry 1842 (class 2604 OID 1077324)
-- Dependencies: 1433 1432 1433
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists_rel_release ALTER COLUMN id SET DEFAULT nextval('artists_rel_release_id_seq'::regclass);


--
-- TOC entry 1843 (class 2604 OID 1077331)
-- Dependencies: 1434 1435 1435
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists_rel_tag ALTER COLUMN id SET DEFAULT nextval('artists_rel_tag_id_seq'::regclass);


--
-- TOC entry 1889 (class 2604 OID 1099869)
-- Dependencies: 1471 1470 1471
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists_stats ALTER COLUMN id SET DEFAULT nextval('artists_stats_id_seq'::regclass);


--
-- TOC entry 1898 (class 2604 OID 1100410)
-- Dependencies: 1478 1477 1478
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE artists_wiki ALTER COLUMN id SET DEFAULT nextval('artists_wiki_id_seq'::regclass);


--
-- TOC entry 1867 (class 2604 OID 1077440)
-- Dependencies: 1458 1459 1459
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE gear ALTER COLUMN id SET DEFAULT nextval('gear_id_seq'::regclass);


--
-- TOC entry 1861 (class 2604 OID 1077412)
-- Dependencies: 1453 1452 1453
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE labels ALTER COLUMN id SET DEFAULT nextval('labels_id_seq'::regclass);


--
-- TOC entry 1847 (class 2604 OID 1077353)
-- Dependencies: 1441 1440 1441
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE labels_rel_tag ALTER COLUMN id SET DEFAULT nextval('labels_rel_tag_id_seq'::regclass);


--
-- TOC entry 1864 (class 2604 OID 1077421)
-- Dependencies: 1455 1454 1455
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE members ALTER COLUMN id SET DEFAULT nextval('members_id_seq'::regclass);


--
-- TOC entry 1850 (class 2604 OID 1077368)
-- Dependencies: 1445 1444 1445
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE members_rel_gear ALTER COLUMN id SET DEFAULT nextval('members_rel_gear_id_seq'::regclass);


--
-- TOC entry 1849 (class 2604 OID 1077361)
-- Dependencies: 1442 1443 1443
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE members_rel_role ALTER COLUMN id SET DEFAULT nextval('members_rel_role_id_seq'::regclass);


--
-- TOC entry 1884 (class 2604 OID 1085385)
-- Dependencies: 1468 1469 1469
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE record_types ALTER COLUMN id SET DEFAULT nextval('record_types_id_seq'::regclass);


--
-- TOC entry 1856 (class 2604 OID 1077396)
-- Dependencies: 1448 1449 1449
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE releases ALTER COLUMN id SET DEFAULT nextval('releases_id_seq'::regclass);


--
-- TOC entry 1910 (class 2604 OID 1125521)
-- Dependencies: 1489 1490 1490
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE releases_comments ALTER COLUMN id SET DEFAULT nextval('releases_comments_id_seq'::regclass);


--
-- TOC entry 1883 (class 2604 OID 1085376)
-- Dependencies: 1466 1467 1467
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE releases_rel_label ALTER COLUMN id SET DEFAULT nextval('releases_rel_label_id_seq'::regclass);


--
-- TOC entry 1851 (class 2604 OID 1077375)
-- Dependencies: 1446 1447 1447
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE releases_rel_tag ALTER COLUMN id SET DEFAULT nextval('releases_rel_tag_id_seq'::regclass);


--
-- TOC entry 1891 (class 2604 OID 1099955)
-- Dependencies: 1472 1473 1473
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE releases_stats ALTER COLUMN id SET DEFAULT nextval('releases_stats_id_seq'::regclass);


--
-- TOC entry 1907 (class 2604 OID 1125488)
-- Dependencies: 1488 1487 1488
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE releases_wiki ALTER COLUMN id SET DEFAULT nextval('releases_wiki_id_seq'::regclass);


--
-- TOC entry 1866 (class 2604 OID 1077430)
-- Dependencies: 1456 1457 1457
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE roles ALTER COLUMN id SET DEFAULT nextval('roles_id_seq'::regclass);


--
-- TOC entry 1859 (class 2604 OID 1077403)
-- Dependencies: 1450 1451 1451
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE tags ALTER COLUMN id SET DEFAULT nextval('tags_id_seq'::regclass);


--
-- TOC entry 1906 (class 2604 OID 1103019)
-- Dependencies: 1485 1484 1485
-- Name: id; Type: DEFAULT; Schema: mtl; Owner: -
--

ALTER TABLE watch_artists ALTER COLUMN id SET DEFAULT nextval('watch_artists_id_seq'::regclass);


SET search_path = sys, pg_catalog;

--
-- TOC entry 1923 (class 2604 OID 1220610)
-- Dependencies: 1504 1505 1505
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE geoip ALTER COLUMN id SET DEFAULT nextval('geoip_id_seq'::regclass);


--
-- TOC entry 1916 (class 2604 OID 1198369)
-- Dependencies: 1495 1496 1496
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE lastfm_cache ALTER COLUMN id SET DEFAULT nextval('"sys.lastfm_cache_id_seq"'::regclass);


--
-- TOC entry 1904 (class 2604 OID 1100789)
-- Dependencies: 1481 1482 1482
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE log ALTER COLUMN id SET DEFAULT nextval('log_id_seq'::regclass);


--
-- TOC entry 1918 (class 2604 OID 1199016)
-- Dependencies: 1497 1498 1498
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE mail_log ALTER COLUMN id SET DEFAULT nextval('mail_log_id_seq'::regclass);


--
-- TOC entry 1896 (class 2604 OID 1100157)
-- Dependencies: 1475 1476 1476
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE news ALTER COLUMN id SET DEFAULT nextval('news_id_seq'::regclass);


--
-- TOC entry 1920 (class 2604 OID 1220599)
-- Dependencies: 1502 1501 1502
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE tasks ALTER COLUMN id SET DEFAULT nextval('tasks_id_seq'::regclass);


--
-- TOC entry 1869 (class 2604 OID 1085212)
-- Dependencies: 1460 1461 1461
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE todo ALTER COLUMN id SET DEFAULT nextval('todo_id_seq'::regclass);


--
-- TOC entry 1913 (class 2604 OID 1127327)
-- Dependencies: 1492 1491 1492
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE userpics ALTER COLUMN id SET DEFAULT nextval('userpics_id_seq'::regclass);


--
-- TOC entry 1874 (class 2604 OID 1085287)
-- Dependencies: 1463 1462 1463
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE users ALTER COLUMN id SET DEFAULT nextval('users_id_seq'::regclass);


--
-- TOC entry 1914 (class 2604 OID 1192170)
-- Dependencies: 1493 1494 1494
-- Name: id; Type: DEFAULT; Schema: sys; Owner: -
--

ALTER TABLE visits ALTER COLUMN id SET DEFAULT nextval('visits_id_seq'::regclass);


SET search_path = mtl, pg_catalog;

--
-- TOC entry 1955 (class 2606 OID 1077398)
-- Dependencies: 1449 1449
-- Name: albums_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY releases
    ADD CONSTRAINT albums_pkey PRIMARY KEY (id);


--
-- TOC entry 2001 (class 2606 OID 1100509)
-- Dependencies: 1480 1480
-- Name: artists_comments_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY artists_comments
    ADD CONSTRAINT artists_comments_pkey PRIMARY KEY (id);


--
-- TOC entry 1979 (class 2606 OID 1085319)
-- Dependencies: 1465 1465
-- Name: artists_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY artists
    ADD CONSTRAINT artists_pkey PRIMARY KEY (id);


--
-- TOC entry 1935 (class 2606 OID 1077348)
-- Dependencies: 1439 1439 1439 1439
-- Name: artists_rel_label_artist_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY artists_rel_label
    ADD CONSTRAINT artists_rel_label_artist_id_key UNIQUE (artist_id, label_id, year);


--
-- TOC entry 1925 (class 2606 OID 1077326)
-- Dependencies: 1433 1433 1433
-- Name: artists_rel_release_artist_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY artists_rel_release
    ADD CONSTRAINT artists_rel_release_artist_id_key UNIQUE (artist_id, release_id);


--
-- TOC entry 1929 (class 2606 OID 1077334)
-- Dependencies: 1435 1435 1435
-- Name: artists_rel_tag_artist_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY artists_rel_tag
    ADD CONSTRAINT artists_rel_tag_artist_id_key UNIQUE (artist_id, tag_id);


--
-- TOC entry 1996 (class 2606 OID 1100417)
-- Dependencies: 1478 1478
-- Name: artists_wiki_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY artists_wiki
    ADD CONSTRAINT artists_wiki_pkey PRIMARY KEY (id);


--
-- TOC entry 1973 (class 2606 OID 1077445)
-- Dependencies: 1459 1459
-- Name: gear_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY gear
    ADD CONSTRAINT gear_pkey PRIMARY KEY (id);


--
-- TOC entry 1961 (class 2606 OID 1077416)
-- Dependencies: 1453 1453
-- Name: labels_name_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY labels
    ADD CONSTRAINT labels_name_key UNIQUE (name);


--
-- TOC entry 1963 (class 2606 OID 1077414)
-- Dependencies: 1453 1453
-- Name: labels_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY labels
    ADD CONSTRAINT labels_pkey PRIMARY KEY (id);


--
-- TOC entry 1941 (class 2606 OID 1077356)
-- Dependencies: 1441 1441 1441
-- Name: labels_rel_tag_label_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY labels_rel_tag
    ADD CONSTRAINT labels_rel_tag_label_id_key UNIQUE (label_id, tag_id);


--
-- TOC entry 1965 (class 2606 OID 1077425)
-- Dependencies: 1455 1455
-- Name: members_name_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY members
    ADD CONSTRAINT members_name_key UNIQUE (name);


--
-- TOC entry 1967 (class 2606 OID 1077423)
-- Dependencies: 1455 1455
-- Name: members_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY members
    ADD CONSTRAINT members_pkey PRIMARY KEY (id);


--
-- TOC entry 1949 (class 2606 OID 1077370)
-- Dependencies: 1445 1445 1445
-- Name: members_rel_gear_member_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY members_rel_gear
    ADD CONSTRAINT members_rel_gear_member_id_key UNIQUE (member_id, gear_id);


--
-- TOC entry 1945 (class 2606 OID 1077363)
-- Dependencies: 1443 1443 1443 1443
-- Name: members_rel_role_member_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY members_rel_role
    ADD CONSTRAINT members_rel_role_member_id_key UNIQUE (member_id, role_id, year);


--
-- TOC entry 1985 (class 2606 OID 1085392)
-- Dependencies: 1469 1469
-- Name: record_types_name_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY record_types
    ADD CONSTRAINT record_types_name_key UNIQUE (name);


--
-- TOC entry 1987 (class 2606 OID 1085390)
-- Dependencies: 1469 1469
-- Name: record_types_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY record_types
    ADD CONSTRAINT record_types_pkey PRIMARY KEY (id);


--
-- TOC entry 2022 (class 2606 OID 1125528)
-- Dependencies: 1490 1490
-- Name: releases_comments_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY releases_comments
    ADD CONSTRAINT releases_comments_pkey PRIMARY KEY (id);


--
-- TOC entry 1983 (class 2606 OID 1085378)
-- Dependencies: 1467 1467 1467
-- Name: releases_rel_label_release_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY releases_rel_label
    ADD CONSTRAINT releases_rel_label_release_id_key UNIQUE (release_id, label_id);


--
-- TOC entry 1953 (class 2606 OID 1077378)
-- Dependencies: 1447 1447 1447
-- Name: releases_rel_tag_release_id_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY releases_rel_tag
    ADD CONSTRAINT releases_rel_tag_release_id_key UNIQUE (release_id, tag_id);


--
-- TOC entry 1992 (class 2606 OID 1099961)
-- Dependencies: 1473 1473
-- Name: releases_stats_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY releases_stats
    ADD CONSTRAINT releases_stats_pkey PRIMARY KEY (release_id);


--
-- TOC entry 2018 (class 2606 OID 1125495)
-- Dependencies: 1488 1488
-- Name: releases_wiki_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY releases_wiki
    ADD CONSTRAINT releases_wiki_pkey PRIMARY KEY (id);


--
-- TOC entry 1969 (class 2606 OID 1077435)
-- Dependencies: 1457 1457
-- Name: roles_name_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY roles
    ADD CONSTRAINT roles_name_key UNIQUE (name);


--
-- TOC entry 1971 (class 2606 OID 1100659)
-- Dependencies: 1457 1457
-- Name: roles_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY roles
    ADD CONSTRAINT roles_pkey PRIMARY KEY (id);


--
-- TOC entry 1957 (class 2606 OID 1077407)
-- Dependencies: 1451 1451
-- Name: tags_name_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY tags
    ADD CONSTRAINT tags_name_key UNIQUE (name);


--
-- TOC entry 1959 (class 2606 OID 1077405)
-- Dependencies: 1451 1451
-- Name: tags_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY tags
    ADD CONSTRAINT tags_pkey PRIMARY KEY (id);


--
-- TOC entry 2011 (class 2606 OID 1103021)
-- Dependencies: 1485 1485
-- Name: watch_artists_pkey; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY watch_artists
    ADD CONSTRAINT watch_artists_pkey PRIMARY KEY (id);


--
-- TOC entry 2013 (class 2606 OID 1103023)
-- Dependencies: 1485 1485 1485
-- Name: watch_artists_unique_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY watch_artists
    ADD CONSTRAINT watch_artists_unique_key UNIQUE (user_id, artist_id);


--
-- TOC entry 2007 (class 2606 OID 1103002)
-- Dependencies: 1483 1483 1483
-- Name: watch_releases_unique_key; Type: CONSTRAINT; Schema: mtl; Owner: -; Tablespace: 
--

ALTER TABLE ONLY watch_releases
    ADD CONSTRAINT watch_releases_unique_key UNIQUE (user_id, release_id);


SET search_path = sys, pg_catalog;

--
-- TOC entry 2026 (class 2606 OID 1220615)
-- Dependencies: 1505 1505
-- Name: geoip_pkey; Type: CONSTRAINT; Schema: sys; Owner: -; Tablespace: 
--

ALTER TABLE ONLY geoip
    ADD CONSTRAINT geoip_pkey PRIMARY KEY (net);


--
-- TOC entry 1994 (class 2606 OID 1100163)
-- Dependencies: 1476 1476
-- Name: news_pkey; Type: CONSTRAINT; Schema: sys; Owner: -; Tablespace: 
--

ALTER TABLE ONLY news
    ADD CONSTRAINT news_pkey PRIMARY KEY (id);


--
-- TOC entry 2024 (class 2606 OID 1218997)
-- Dependencies: 1496 1496 1496
-- Name: unique_query; Type: CONSTRAINT; Schema: sys; Owner: -; Tablespace: 
--

ALTER TABLE ONLY lastfm_cache
    ADD CONSTRAINT unique_query UNIQUE (method, params);


--
-- TOC entry 1975 (class 2606 OID 1085299)
-- Dependencies: 1463 1463
-- Name: users_email_key; Type: CONSTRAINT; Schema: sys; Owner: -; Tablespace: 
--

ALTER TABLE ONLY users
    ADD CONSTRAINT users_email_key UNIQUE (email);


--
-- TOC entry 1977 (class 2606 OID 1085295)
-- Dependencies: 1463 1463
-- Name: users_pkey; Type: CONSTRAINT; Schema: sys; Owner: -; Tablespace: 
--

ALTER TABLE ONLY users
    ADD CONSTRAINT users_pkey PRIMARY KEY (id);


SET search_path = mtl, pg_catalog;

--
-- TOC entry 2002 (class 1259 OID 1100533)
-- Dependencies: 1480
-- Name: fki_artists_comments_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_comments_artist_id_key ON artists_comments USING btree (artist_id);


--
-- TOC entry 2003 (class 1259 OID 1100539)
-- Dependencies: 1480
-- Name: fki_artists_comments_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_comments_user_id_key ON artists_comments USING btree (added_by_user_id);


--
-- TOC entry 1936 (class 1259 OID 1100527)
-- Dependencies: 1439
-- Name: fki_artists_rel_label_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_label_artist_id_key ON artists_rel_label USING btree (artist_id);


--
-- TOC entry 1937 (class 1259 OID 1100526)
-- Dependencies: 1439
-- Name: fki_artists_rel_label_label_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_label_label_id_key ON artists_rel_label USING btree (label_id);


--
-- TOC entry 1932 (class 1259 OID 1100545)
-- Dependencies: 1437
-- Name: fki_artists_rel_member_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_member_artist_id_key ON artists_rel_member USING btree (artist_id);


--
-- TOC entry 1933 (class 1259 OID 1100551)
-- Dependencies: 1437
-- Name: fki_artists_rel_member_member_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_member_member_id_key ON artists_rel_member USING btree (member_id);


--
-- TOC entry 1926 (class 1259 OID 1100557)
-- Dependencies: 1433
-- Name: fki_artists_rel_release_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_release_artist_id_key ON artists_rel_release USING btree (artist_id);


--
-- TOC entry 1927 (class 1259 OID 1100563)
-- Dependencies: 1433
-- Name: fki_artists_rel_release_release_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_release_release_id_key ON artists_rel_release USING btree (release_id);


--
-- TOC entry 1930 (class 1259 OID 1100569)
-- Dependencies: 1435
-- Name: fki_artists_rel_tag_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_tag_artist_id_key ON artists_rel_tag USING btree (artist_id);


--
-- TOC entry 1931 (class 1259 OID 1100575)
-- Dependencies: 1435
-- Name: fki_artists_rel_tag_tag_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_rel_tag_tag_id_key ON artists_rel_tag USING btree (tag_id);


--
-- TOC entry 1988 (class 1259 OID 1100591)
-- Dependencies: 1471
-- Name: fki_artists_stats_added_by_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_stats_added_by_user_id_key ON artists_stats USING btree (added_by_user_id);


--
-- TOC entry 1989 (class 1259 OID 1100598)
-- Dependencies: 1471
-- Name: fki_artists_stats_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_stats_artist_id_key ON artists_stats USING btree (artist_id);


--
-- TOC entry 1990 (class 1259 OID 1100597)
-- Dependencies: 1471
-- Name: fki_artists_stats_changed_by_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_stats_changed_by_user_id_key ON artists_stats USING btree (changed_by_user_id);


--
-- TOC entry 1997 (class 1259 OID 1100611)
-- Dependencies: 1478
-- Name: fki_artists_wiki_added_by_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_wiki_added_by_user_id_key ON artists_wiki USING btree (added_by_user_id);


--
-- TOC entry 1998 (class 1259 OID 1100605)
-- Dependencies: 1478
-- Name: fki_artists_wiki_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_wiki_artist_id_key ON artists_wiki USING btree (artist_id);


--
-- TOC entry 1999 (class 1259 OID 1100617)
-- Dependencies: 1478
-- Name: fki_artists_wiki_changed_by_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_artists_wiki_changed_by_user_id_key ON artists_wiki USING btree (changed_by_user_id);


--
-- TOC entry 1938 (class 1259 OID 1100628)
-- Dependencies: 1441
-- Name: fki_labels_rel_tag_label_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_labels_rel_tag_label_id_key ON labels_rel_tag USING btree (label_id);


--
-- TOC entry 1939 (class 1259 OID 1100634)
-- Dependencies: 1441
-- Name: fki_labels_rel_tag_tag_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_labels_rel_tag_tag_id_key ON labels_rel_tag USING btree (tag_id);


--
-- TOC entry 1946 (class 1259 OID 1100645)
-- Dependencies: 1445
-- Name: fki_member_rel_gear_gear_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_member_rel_gear_gear_id_fkey ON members_rel_gear USING btree (gear_id);


--
-- TOC entry 1947 (class 1259 OID 1100646)
-- Dependencies: 1445
-- Name: fki_member_rel_gear_member_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_member_rel_gear_member_id_fkey ON members_rel_gear USING btree (member_id);


--
-- TOC entry 1942 (class 1259 OID 1100657)
-- Dependencies: 1443
-- Name: fki_members_rel_role_member_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_members_rel_role_member_id_fkey ON members_rel_role USING btree (member_id);


--
-- TOC entry 1943 (class 1259 OID 1100665)
-- Dependencies: 1443
-- Name: fki_members_rel_role_role_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_members_rel_role_role_id_fkey ON members_rel_role USING btree (role_id);


--
-- TOC entry 2019 (class 1259 OID 1125539)
-- Dependencies: 1490
-- Name: fki_releases_comments_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_comments_artist_id_key ON releases_comments USING btree (release_id);


--
-- TOC entry 2020 (class 1259 OID 1125540)
-- Dependencies: 1490
-- Name: fki_releases_comments_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_comments_user_id_key ON releases_comments USING btree (added_by_user_id);


--
-- TOC entry 1980 (class 1259 OID 1100677)
-- Dependencies: 1467
-- Name: fki_releases_rel_label_label_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_rel_label_label_id_fkey ON releases_rel_label USING btree (label_id);


--
-- TOC entry 1981 (class 1259 OID 1100671)
-- Dependencies: 1467
-- Name: fki_releases_rel_label_release_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_rel_label_release_id_fkey ON releases_rel_label USING btree (release_id);


--
-- TOC entry 1950 (class 1259 OID 1100683)
-- Dependencies: 1447
-- Name: fki_releases_rel_tag_release_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_rel_tag_release_id_fkey ON releases_rel_tag USING btree (release_id);


--
-- TOC entry 1951 (class 1259 OID 1100689)
-- Dependencies: 1447
-- Name: fki_releases_rel_tag_tag_id_fkey; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_rel_tag_tag_id_fkey ON releases_rel_tag USING btree (tag_id);


--
-- TOC entry 2014 (class 1259 OID 1125511)
-- Dependencies: 1488
-- Name: fki_releases_wiki_added_by_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_wiki_added_by_user_id_key ON releases_wiki USING btree (added_by_user_id);


--
-- TOC entry 2015 (class 1259 OID 1125512)
-- Dependencies: 1488
-- Name: fki_releases_wiki_artist_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_wiki_artist_id_key ON releases_wiki USING btree (release_id);


--
-- TOC entry 2016 (class 1259 OID 1125513)
-- Dependencies: 1488
-- Name: fki_releases_wiki_changed_by_user_id_key; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_releases_wiki_changed_by_user_id_key ON releases_wiki USING btree (changed_by_user_id);


--
-- TOC entry 2008 (class 1259 OID 1103035)
-- Dependencies: 1485
-- Name: fki_watch_artists_artist_id; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_watch_artists_artist_id ON watch_artists USING btree (artist_id);


--
-- TOC entry 2009 (class 1259 OID 1103034)
-- Dependencies: 1485
-- Name: fki_watch_artists_user_id; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_watch_artists_user_id ON watch_artists USING btree (user_id);


--
-- TOC entry 2004 (class 1259 OID 1103014)
-- Dependencies: 1483
-- Name: fki_watch_releases_release_id; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_watch_releases_release_id ON watch_releases USING btree (release_id);


--
-- TOC entry 2005 (class 1259 OID 1103013)
-- Dependencies: 1483
-- Name: fki_watch_releases_user_id; Type: INDEX; Schema: mtl; Owner: -; Tablespace: 
--

CREATE INDEX fki_watch_releases_user_id ON watch_releases USING btree (user_id);


--
-- TOC entry 2062 (class 2620 OID 1106648)
-- Dependencies: 1465 59
-- Name: create_artist_stats; Type: TRIGGER; Schema: mtl; Owner: -
--

CREATE TRIGGER create_artist_stats
    AFTER INSERT ON artists
    FOR EACH ROW
    EXECUTE PROCEDURE create_artist_stats();

ALTER TABLE artists DISABLE TRIGGER create_artist_stats;


--
-- TOC entry 2061 (class 2620 OID 1106662)
-- Dependencies: 65 1449
-- Name: create_release_stats; Type: TRIGGER; Schema: mtl; Owner: -
--

CREATE TRIGGER create_release_stats
    AFTER INSERT ON releases
    FOR EACH ROW
    EXECUTE PROCEDURE create_release_stats();

ALTER TABLE releases DISABLE TRIGGER create_release_stats;


--
-- TOC entry 2059 (class 2620 OID 1106660)
-- Dependencies: 1449 64
-- Name: delete_release_relations; Type: TRIGGER; Schema: mtl; Owner: -
--

CREATE TRIGGER delete_release_relations
    AFTER DELETE ON releases
    FOR EACH ROW
    EXECUTE PROCEDURE delete_release_relations();

ALTER TABLE releases DISABLE TRIGGER delete_release_relations;


--
-- TOC entry 2060 (class 2620 OID 1106664)
-- Dependencies: 1449 66
-- Name: delete_release_stats; Type: TRIGGER; Schema: mtl; Owner: -
--

CREATE TRIGGER delete_release_stats
    AFTER DELETE ON releases
    FOR EACH ROW
    EXECUTE PROCEDURE delete_release_stats();

ALTER TABLE releases DISABLE TRIGGER delete_release_stats;


--
-- TOC entry 2049 (class 2606 OID 1199365)
-- Dependencies: 1978 1480 1465
-- Name: artists_comments_artist_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_comments
    ADD CONSTRAINT artists_comments_artist_id_key FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2034 (class 2606 OID 1199370)
-- Dependencies: 1978 1465 1439
-- Name: artists_rel_label_artist_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_label
    ADD CONSTRAINT artists_rel_label_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2033 (class 2606 OID 1100521)
-- Dependencies: 1439 1453 1962
-- Name: artists_rel_label_label_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_label
    ADD CONSTRAINT artists_rel_label_label_id_key FOREIGN KEY (label_id) REFERENCES labels(id);


--
-- TOC entry 2032 (class 2606 OID 1199375)
-- Dependencies: 1978 1437 1465
-- Name: artists_rel_member_artist_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_member
    ADD CONSTRAINT artists_rel_member_artist_id_key FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2031 (class 2606 OID 1100546)
-- Dependencies: 1966 1437 1455
-- Name: artists_rel_member_member_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_member
    ADD CONSTRAINT artists_rel_member_member_id_key FOREIGN KEY (member_id) REFERENCES members(id);


--
-- TOC entry 2028 (class 2606 OID 1199380)
-- Dependencies: 1978 1465 1433
-- Name: artists_rel_release_artist_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_release
    ADD CONSTRAINT artists_rel_release_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2027 (class 2606 OID 1194851)
-- Dependencies: 1449 1433 1954
-- Name: artists_rel_release_release_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_release
    ADD CONSTRAINT artists_rel_release_release_id_key FOREIGN KEY (release_id) REFERENCES releases(id) ON DELETE CASCADE;


--
-- TOC entry 2029 (class 2606 OID 1199385)
-- Dependencies: 1465 1435 1978
-- Name: artists_rel_tag_artist_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_tag
    ADD CONSTRAINT artists_rel_tag_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2030 (class 2606 OID 1100570)
-- Dependencies: 1435 1451 1958
-- Name: artists_rel_tag_tag_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_rel_tag
    ADD CONSTRAINT artists_rel_tag_tag_id_key FOREIGN KEY (tag_id) REFERENCES tags(id);


--
-- TOC entry 2045 (class 2606 OID 1199390)
-- Dependencies: 1465 1471 1978
-- Name: artists_stats_artist_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_stats
    ADD CONSTRAINT artists_stats_artist_id_key FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2046 (class 2606 OID 1100606)
-- Dependencies: 1478 1976 1463
-- Name: artists_wiki_added_by_user_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_wiki
    ADD CONSTRAINT artists_wiki_added_by_user_id_key FOREIGN KEY (added_by_user_id) REFERENCES sys.users(id);


--
-- TOC entry 2048 (class 2606 OID 1199395)
-- Dependencies: 1978 1478 1465
-- Name: artists_wiki_artist_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_wiki
    ADD CONSTRAINT artists_wiki_artist_id_key FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2047 (class 2606 OID 1100612)
-- Dependencies: 1976 1463 1478
-- Name: artists_wiki_changed_by_user_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY artists_wiki
    ADD CONSTRAINT artists_wiki_changed_by_user_id_key FOREIGN KEY (changed_by_user_id) REFERENCES sys.users(id);


--
-- TOC entry 2035 (class 2606 OID 1100623)
-- Dependencies: 1441 1453 1962
-- Name: labels_rel_tag_label_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY labels_rel_tag
    ADD CONSTRAINT labels_rel_tag_label_id_fkey FOREIGN KEY (label_id) REFERENCES labels(id);


--
-- TOC entry 2036 (class 2606 OID 1100629)
-- Dependencies: 1451 1441 1958
-- Name: labels_rel_tag_tag_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY labels_rel_tag
    ADD CONSTRAINT labels_rel_tag_tag_id_key FOREIGN KEY (tag_id) REFERENCES tags(id);


--
-- TOC entry 2040 (class 2606 OID 1100640)
-- Dependencies: 1972 1459 1445
-- Name: member_rel_gear_gear_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY members_rel_gear
    ADD CONSTRAINT member_rel_gear_gear_id_fkey FOREIGN KEY (gear_id) REFERENCES gear(id);


--
-- TOC entry 2039 (class 2606 OID 1100635)
-- Dependencies: 1445 1966 1455
-- Name: members_rel_gear_member_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY members_rel_gear
    ADD CONSTRAINT members_rel_gear_member_id_fkey FOREIGN KEY (member_id) REFERENCES members(id);


--
-- TOC entry 2037 (class 2606 OID 1100652)
-- Dependencies: 1455 1966 1443
-- Name: members_rel_role_member_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY members_rel_role
    ADD CONSTRAINT members_rel_role_member_id_fkey FOREIGN KEY (member_id) REFERENCES members(id);


--
-- TOC entry 2038 (class 2606 OID 1100660)
-- Dependencies: 1970 1443 1457
-- Name: members_rel_role_role_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY members_rel_role
    ADD CONSTRAINT members_rel_role_role_id_fkey FOREIGN KEY (role_id) REFERENCES roles(id);


--
-- TOC entry 2057 (class 2606 OID 1194856)
-- Dependencies: 1490 1449 1954
-- Name: releases_comments_release_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_comments
    ADD CONSTRAINT releases_comments_release_id_key FOREIGN KEY (release_id) REFERENCES releases(id) ON DELETE CASCADE;


--
-- TOC entry 2043 (class 2606 OID 1100672)
-- Dependencies: 1467 1453 1962
-- Name: releases_rel_label_label_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_rel_label
    ADD CONSTRAINT releases_rel_label_label_id_fkey FOREIGN KEY (label_id) REFERENCES labels(id);


--
-- TOC entry 2044 (class 2606 OID 1194911)
-- Dependencies: 1449 1467 1954
-- Name: releases_rel_label_release_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_rel_label
    ADD CONSTRAINT releases_rel_label_release_id_fkey FOREIGN KEY (release_id) REFERENCES releases(id) ON DELETE CASCADE;


--
-- TOC entry 2042 (class 2606 OID 1194916)
-- Dependencies: 1954 1447 1449
-- Name: releases_rel_tag_release_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_rel_tag
    ADD CONSTRAINT releases_rel_tag_release_id_fkey FOREIGN KEY (release_id) REFERENCES releases(id) ON DELETE CASCADE;


--
-- TOC entry 2041 (class 2606 OID 1100684)
-- Dependencies: 1451 1958 1447
-- Name: releases_rel_tag_tag_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_rel_tag
    ADD CONSTRAINT releases_rel_tag_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES tags(id);


--
-- TOC entry 2055 (class 2606 OID 1125496)
-- Dependencies: 1976 1463 1488
-- Name: releases_wiki_added_by_user_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_wiki
    ADD CONSTRAINT releases_wiki_added_by_user_id_key FOREIGN KEY (added_by_user_id) REFERENCES sys.users(id);


--
-- TOC entry 2056 (class 2606 OID 1125506)
-- Dependencies: 1463 1488 1976
-- Name: releases_wiki_changed_by_user_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_wiki
    ADD CONSTRAINT releases_wiki_changed_by_user_id_key FOREIGN KEY (changed_by_user_id) REFERENCES sys.users(id);


--
-- TOC entry 2054 (class 2606 OID 1199346)
-- Dependencies: 1954 1488 1449
-- Name: releases_wiki_release_id_key; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY releases_wiki
    ADD CONSTRAINT releases_wiki_release_id_key FOREIGN KEY (release_id) REFERENCES releases(id) ON DELETE CASCADE;


--
-- TOC entry 2053 (class 2606 OID 1199400)
-- Dependencies: 1978 1485 1465
-- Name: watch_artists_artist_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY watch_artists
    ADD CONSTRAINT watch_artists_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE;


--
-- TOC entry 2052 (class 2606 OID 1103024)
-- Dependencies: 1976 1463 1485
-- Name: watch_artists_user_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY watch_artists
    ADD CONSTRAINT watch_artists_user_id_fkey FOREIGN KEY (user_id) REFERENCES sys.users(id);


--
-- TOC entry 2050 (class 2606 OID 1199351)
-- Dependencies: 1483 1449 1954
-- Name: watch_releases_release_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY watch_releases
    ADD CONSTRAINT watch_releases_release_id_fkey FOREIGN KEY (release_id) REFERENCES releases(id) ON DELETE CASCADE;


--
-- TOC entry 2051 (class 2606 OID 1103003)
-- Dependencies: 1483 1463 1976
-- Name: watch_releases_user_id_fkey; Type: FK CONSTRAINT; Schema: mtl; Owner: -
--

ALTER TABLE ONLY watch_releases
    ADD CONSTRAINT watch_releases_user_id_fkey FOREIGN KEY (user_id) REFERENCES sys.users(id);


SET search_path = sys, pg_catalog;

--
-- TOC entry 2058 (class 2606 OID 1127328)
-- Dependencies: 1976 1492 1463
-- Name: userpics_user_id_key; Type: FK CONSTRAINT; Schema: sys; Owner: -
--

ALTER TABLE ONLY userpics
    ADD CONSTRAINT userpics_user_id_key FOREIGN KEY (user_id) REFERENCES users(id);


--
-- TOC entry 2067 (class 0 OID 0)
-- Dependencies: 6
-- Name: public; Type: ACL; Schema: -; Owner: -
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM pgsql;
GRANT ALL ON SCHEMA public TO pgsql;
GRANT ALL ON SCHEMA public TO PUBLIC;


SET search_path = mtl, pg_catalog;

--
-- TOC entry 2068 (class 0 OID 0)
-- Dependencies: 50
-- Name: add_artist_comment(integer, integer, text, cidr); Type: ACL; Schema: mtl; Owner: -
--

